package com.xuesaieducation.zshddemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(value = {R.id.btn, R.id.offline})
    public void onViewClicked(View v)
    {
        switch (v.getId())
        {
            case R.id.btn:
                Intent it = new Intent(this, GenSeePlayActivity.class);
                it.putExtra("isLive", true);
                it.putExtra("number", "88777165");
                it.putExtra("serviceType", GenSeePlayActivity.TYPE_TRAINING);
                it.putExtra("joinPwd", "888888");
                startActivity(it);

//                Intent it2 = new Intent(this, GenSeePlayActivity.class);
//                it2.putExtra("isLive", false);
//                it2.putExtra("number", "52586001");
//                it2.putExtra("serviceType", GenSeePlayActivity.TYPE_TRAINING);
//                it2.putExtra("joinPwd", "111111");
//                startActivity(it2);
                break;
            case R.id.offline:
//                Intent it2 = new Intent(this, GenSeePlayActivity.class);
//                it2.putExtra("isLive", false);
//                it2.putExtra("number", "52586001");
//                it2.putExtra("serviceType", GenSeePlayActivity.TYPE_TRAINING);
//                it2.putExtra("joinPwd", "111111");
//                startActivity(it2);

                Intent ii = new Intent(this, DownloadListActivity.class);
                startActivity(ii);
                break;
        }

//        Intent it = new Intent(this, GenSeePlayActivity.class);
//        it.putExtra("isLive", true);
//        it.putExtra("number", "88777165");
//        it.putExtra("serviceType", GenSeePlayActivity.TYPE_TRAINING);
//        it.putExtra("joinPwd", "888888");
//        startActivity(it);
//        long timeMillis = System.currentTimeMillis();
//        addCalenderEvent(getApplicationContext(), "课程提醒:您明天有课程要参加，请不要忘记呦", "有课程即将开始，请及时查看", timeMillis);
    }
/*

    private static String CALANDER_URL = "content://com.android.calendar/calendars";
    private static String CALANDER_EVENT_URL = "content://com.android.calendar/events";
    private static String CALANDER_REMIDER_URL = "content://com.android.calendar/reminders";

    private static String CALENDARS_NAME = "Mytest";
    private static String CALENDARS_ACCOUNT_NAME = "mytest";
    private static String CALENDARS_ACCOUNT_TYPE = "calender.test";
    private static String CALENDARS_DISPLAY_NAME = "Mytest";

    //检查是否有现在存在的账户。存在则返回账户id，否则返回-1
    private static int checkCalendarAccount(Context context)
    {
        Cursor userCursor = context.getContentResolver().query(Uri.parse(CALANDER_URL),
                null, null, null, null);
        try
        {
            if (userCursor == null)
            {
                return -1;
            } else
            {
                int count = userCursor.getCount();
                if (count > 0)
                {  //存在现有账户，取第一个账户的id返回
                    userCursor.moveToFirst();
                    return userCursor.getInt(userCursor.getColumnIndex(CalendarContract.Calendars._ID));
                } else
                {
                    return -1;
                }
            }
        } finally
        {
            if (userCursor != null)
            {
                userCursor.close();
            }
        }
    }

    //添加日历账户
    private static long addCalendarAccount(Context context)
    {
        TimeZone timeZone = TimeZone.getDefault();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Calendars.NAME, CALENDARS_NAME);
        values.put(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_DISPLAY_NAME);
        values.put(CalendarContract.Calendars.VISIBLE, 1);
        values.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);
        values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
        values.put(CalendarContract.Calendars.OWNER_ACCOUNT, CALENDARS_ACCOUNT_NAME);
        values.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);
        Uri calendarUri = Uri.parse(CALANDER_URL);
        calendarUri = calendarUri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, CALENDARS_ACCOUNT_NAME).appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CALENDARS_ACCOUNT_TYPE).build();

        Uri result = context.getContentResolver().insert(calendarUri, values);
        long id = result == null ? -1 : ContentUris.parseId(result);
        return id;
    }

    //检查是否已经添加了日历账户，如果没有添加先添加一个日历账户再查询
    private static int checkAndAddCalendarAccount(Context context)
    {
        int oldId = checkCalendarAccount(context);
        if (oldId >= 0)
        {
            return oldId;
        } else
        {
            long addId = addCalendarAccount(context);
            if (addId >= 0)
            {
                return checkCalendarAccount(context);
            } else
            {
                return -1;
            }

        }
    }

    //添加日历事件
    public static void addCalenderEvent(Context context, String title, String description, long beginTime)
    {
        //获取日历账户的id
        int calId = checkAndAddCalendarAccount(context);
        if (calId < 0)
        {
            //获取日历账户id失败，直接返回，添加日历事件失败
            return;
        }
        ContentValues values = new ContentValues();
        values.put("title", title);
        values.put("description", description);
        values.put("calendar_id", calId);
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(beginTime);  //设置开始时间
        long start = mCalendar.getTime().getTime();
        mCalendar.setTimeInMillis(start + 600000);//设置终止时间
        long end = mCalendar.getTime().getTime();
        values.put(CalendarContract.Events.DTSTART, start);
        values.put(CalendarContract.Events.DTEND, end);
        values.put(CalendarContract.Events.HAS_ALARM, true);//设置有闹钟提醒
        values.put(CalendarContract.Events.EVENT_TIMEZONE, "Asia/Shanghai"); //这个是时区，必须有
        values.put(CalendarContract.Events.STATUS, 1);
        Uri newEvent = context.getContentResolver().insert(Uri.parse(CALANDER_EVENT_URL), values);

        if (newEvent == null)
        {
            //添加日历事件失败直接返回
            Log.d("tag", "添加失败！");
            return;
        }

        ContentValues values1new = new ContentValues();
        values1new.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
        values1new.put(CalendarContract.Reminders.MINUTES, 1440);  //提前一天有提醒  1440
        values1new.put(CalendarContract.Reminders.METHOD, 1);

        Uri uri = context.getContentResolver().insert(Uri.parse(CALANDER_REMIDER_URL), values1new);
        if (uri == null)
        {
            //添加闹钟提醒失败直接返回
            Log.d("tag", "添加失败！");
            return;
        } else
        {
            Log.d("tag", "添加成功！" + uri);
        }
    }
*/

}